package com.workflow.service.impl;

import java.util.List;

import com.base.bean.Cardinfo;
import com.common.bean.Datadict;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.workflow.bean.Exitcard;
import com.workflow.bean.Exitdeptdetail;
import com.workflow.service.ExitdeptdetailService;

public class ExitdeptdetailServiceImpl extends AbstractManager implements
		ExitdeptdetailService {

	public void addExitdetailno(Exitdeptdetail exitdeptdetail) {
		String hql = "select count(*) from Exitdeptdetail edd where edd.exitdept.exitno = ?";
		long count = 0;
		count = (Long)this.getHibernateTemplate().find(hql,exitdeptdetail.getExitdept().getExitno()).get(0);
		count %= 1000L;
		
//		Long num = Long.parseLong(DateHelper.dateToStr(new Date(), "yyyyMMddHmsS"));
//		num *= 1000L;		
//		num += count;
		
		String exitno = exitdeptdetail.getExitdept().getExitno();
		
		String exitdetailno = "TKD-" + exitno.substring(exitno.length()-9, exitno.length()) + count;
		exitdeptdetail.setExitdetailno(exitdetailno);
		
		this.getHibernateTemplate().save(exitdeptdetail);
	}

	@SuppressWarnings("unchecked")
	public List<Exitdeptdetail> findAllExitdetailno() {
		String hql = "from Exitdeptdetail edd order by edd.exitdept.exitno , edd.exitdetailno ";
		return this.getHibernateTemplate().find(hql);
	}

	public Exitdeptdetail findExitdetailno(String exitdetailno) {
		return (Exitdeptdetail) this.getHibernateTemplate().load(Exitdeptdetail.class, exitdetailno);
	}

	public PagerModel findExitdetailnoForPager() {
		String hql = "from Exitdeptdetail edd order by edd.exitdept.exitno , edd.exitdetailno ";
		return searchPaginated(hql);
	}

	public PagerModel findExitdetailnoForPager(String exitno) {
		String hql = "from Exitdeptdetail edd where edd.exitdept.exitno = ?  order by edd.exitdetailno ";
		return searchPaginated(hql,exitno);
	}

	public void removeExitdetailno(String exitdetailno) {
		Exitdeptdetail exitdeptdetail = (Exitdeptdetail) this.getHibernateTemplate().load(Exitdeptdetail.class, exitdetailno);
		if(exitdeptdetail != null) {
			String hql = "from Exitcard ec where ec.exitdeptdetail.exitdetailno = ? order by ec.cardno ";
			List<Exitcard> exitcardlist = this.getHibernateTemplate().find(hql);
			for(Exitcard exitcard : exitcardlist) {
				Cardinfo cardinfo = null;
				cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, exitcard.getCardno());
				Datadict datadictByStatus = new Datadict();
//				置卡状态为库存 状态编号 76l
				datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_STORE);
				cardinfo.setDatadictByStatus(datadictByStatus);
				cardinfo.setBelongto("");
				this.getHibernateTemplate().update(cardinfo);
				this.getHibernateTemplate().delete(exitcard);
			}
			this.getHibernateTemplate().delete(exitdeptdetail);
		}
	}

	public void updateExitdetailno(Exitdeptdetail exitdeptdetail) {
		this.getHibernateTemplate().update(exitdeptdetail);
	}

}
